package com.flute.icrawler.app.task.entity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.flute.icrawler.app.task.ITaskState;

public class FinishedTaskState implements ITaskState {

	private static Logger logger = LoggerFactory
			.getLogger(FinishedTaskState.class);

	@Override
	public ITaskState handle(CrawlTask crawlTask) {
		crawlTask.loadTaskInfo();
		logger.info("this crawlTask is finished! name=" + crawlTask.getName());
		// 周期任务已完成 运行周期点未到进行提交
		if (0 != crawlTask.getIntervalDay()
				&& crawlTask.getFinishTime() != 0
				&& System.currentTimeMillis() > crawlTask.getFinishTime()
						+ crawlTask.getIntervalDay()) {
			// 提交成功
			if (crawlTask.submitCrawlerJob()) {
				return CrawlTask.STATE_RUNNING;
			} else {
				return CrawlTask.STATE_WAITING;
			}

		}
		// 新任务需要进行提交
		else if (0 == crawlTask.getFinishTime()) {
			// 提交成功
			if (crawlTask.submitCrawlerJob()) {
				return CrawlTask.STATE_RUNNING;
			} else {
				return CrawlTask.STATE_WAITING;
			}
		}
		return null;

	}

}
